package cn.imnu.legalaid.service.impl;

import cn.imnu.legalaid.dto.User.PublicUserInfoDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.imnu.legalaid.entity.User;
import cn.imnu.legalaid.service.UserService;
import cn.imnu.legalaid.mapper.UsersMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;


/**
* @author 18498
* @description 针对表【users(用户表)】的数据库操作Service实现
* @createDate 2025-09-19 17:49:09
*/
@Service
public class UserServiceImpl extends ServiceImpl<UsersMapper, User>
    implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return this.getOne(queryWrapper);
    }

    @Override
    public void updateLastLogin(Integer id) {
        User user = new User();
        user.setId(id);
        user.setLastLogin(LocalDateTime.now());
        this.updateById(user);
    }

    @Override
    public User getUserByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return this.getOne(queryWrapper);
    }

    @Override
    public boolean createUser(User user) {
        user.setCreatedAt(LocalDateTime.now());
        user.setUpdatedAt(LocalDateTime.now());
        return this.save(user);
    }

    // 新增公开信息相关方法实现
    @Override
    public PublicUserInfoDTO getPublicUserInfo(Integer userId) {
        if (userId == null) {
            return null;
        }
        return usersMapper.selectPublicUserInfo(userId);
    }

    @Override
    public List<PublicUserInfoDTO> getPublicUserInfoBatch(List<Integer> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return null;
        }
        return usersMapper.selectPublicUserInfoBatch(userIds);
    }

    @Override
    public List<PublicUserInfoDTO> getPublicLawyersInfo(Integer page, Integer size, String specialty) {
        Integer offset = (page - 1) * size;
        return usersMapper.selectPublicLawyersInfo(offset, size, specialty);
    }
}




